package fr.cpe.j2ee.projet.war;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.RowEditEvent;

import fr.cpe.j2ee.projet.ejb.EmployeeSession;
import fr.cpe.j2ee.projet.model.Activity;
import fr.cpe.j2ee.projet.model.Employee;

public class UserTableBean implements Serializable
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Employee> userList;

	@EJB
	private EmployeeSession empSess;

	public UserTableBean()
	{
		userList = new ArrayList<Employee>();
	}

	@PostConstruct
	public void postConstruct()
	{
		for (Employee e : this.empSess.getAll())
		{
			// Modification car on utilise plus que des employee à la place des UserBean
//			UserBean uB = new UserBean(e.getEmpFirstname(), e.getEmpLastname(),
//					e.getEmpUsername(), e.getEmpPassword(), e.getEmpEmail(), e.getEmpAge(),
//					e.getEmpIndate());

			this.userList.add(e);
		}
	}

	public void addUser(UserAdditionBean userAdded)
	{
		/**
		 * Rajouter appel à la méthode des Bean entity pour ajout du nouvel
		 * utilisateur dans la BDD
		 */
		Date d = new Date();
		
		// Ajout du nouvel employé dans la liste d'affichage de la page .xhtml
		this.empSess.addUser(userAdded.getEmail(), userAdded.getUsername(), 
				userAdded.getPassword(), userAdded.getFirstname(), 
				userAdded.getLastname(), userAdded.getAge(), d);
		
		Employee employeeToAdd = new Employee();
		
		employeeToAdd.setEmpAge(userAdded.getAge());
		employeeToAdd.setEmpEmail(userAdded.getEmail());
		employeeToAdd.setEmpFirstname(userAdded.getFirstname());
		employeeToAdd.setEmpIndate(d);
		employeeToAdd.setEmpLastname(userAdded.getLastname());
		employeeToAdd.setEmpPassword(userAdded.getPassword());
		employeeToAdd.setEmpUsername(userAdded.getUsername());
		
		this.userList.add(employeeToAdd);
	}

	public List<Employee> getUserList()
	{
		return userList;
	}

	public void onEdit(RowEditEvent event)
	{
		// Appel à la méthode update() de l'objet Employee pour mettre à jour les champs modifiés
		this.empSess.update(((Employee) event.getObject()));
		
		// Affichage d'un message d'information
		FacesMessage msg = new FacesMessage("User edited",
				((Employee) event.getObject()).toString());
		FacesContext.getCurrentInstance().addMessage(null, msg);
		System.out.println("User edited");
	}

	public void onCancel(RowEditEvent event)
	{
		// Affichage d'un message d'information
		FacesMessage msg = new FacesMessage("User edition cancelled",
				((Employee) event.getObject()).toString());
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}
	
	public List<Activity> getProjectActivities(Employee emp, int projectId)
	{
		List<Activity> projectActivities = new ArrayList<Activity>();
		Set<Activity> activities = emp.getActivities();
		
		for(Activity act : activities)
		{
			if(act.getProject().getProId() == projectId)
			{
				projectActivities.add(act);
			}
		}
		
		return projectActivities;
	}
}